#include "StringSplitter.h"



CStringSplitter::CStringSplitter() : iTextContentHandler(NULL)
{

}

CStringSplitter::~CStringSplitter()
{
	delete iText;
	iText = NULL;
	
	delete iTextContentHandler;
	iTextContentHandler = NULL;
}

CStringSplitter* CStringSplitter::NewL(const TDesC& aDes)
{
	CStringSplitter* self = CStringSplitter::NewLC(aDes);
	CleanupStack::Pop(self);
	
	return self;
}

CStringSplitter* CStringSplitter::NewLC(const TDesC& aDes)
{
	CStringSplitter* self = new(ELeave) CStringSplitter;
	CleanupStack::PushL(self);
	self->ConstructL(aDes);
	
	return self;
}

void CStringSplitter::ConstructL(const TDesC& aDes)
{
	iText = HBufC::NewL(aDes.Length());
	CleanupStack::PushL(iText);
	TPtr ptr = iText->Des();
	
	ptr.Copy(aDes);
	CleanupStack::Pop(1);
	
	iTextContentHandler = CTextContentHandler::NewL(&iOriginTextLink);
}

void CStringSplitter::ParseL()
{
	TInt tt = iText->Length();
	iTextContentHandler->ParseXmlL(*iText);
}

void CStringSplitter::SplitTextBlock(TInt aWidth)
{
	Iterator* it = (Iterator*)&iOriginTextLink;
	it->ResetIterator();
	while(it->HasNext())
		{
		CTextBlock* pBlock = (CTextBlock*)it->GetNext();
		pBlock->SplitTextL(iFormatTextLineLink,aWidth);
		}
}

TSize CStringSplitter::EstimateSize()
	{
	TInt width = 0;
	TInt height = 0;
	
	Iterator* itLine = (Iterator*)&iFormatTextLineLink;
	itLine->ResetIterator();
	while(itLine->HasNext())
		{
		TextLine* pTextLine = (TextLine*)itLine->GetNext();
		if(pTextLine)
			{
			TRect rcLine = pTextLine->GetLineRect();
			if(rcLine.Width() > width)
				width = rcLine.Width();
			
			height += rcLine.Height();
			}
		}
	
	return TSize(width,height);
	}


void CStringSplitter::Layout(const TRect& aRc)
{
	TInt top = aRc.iTl.iY;

	Iterator* itLine = (Iterator*)&iFormatTextLineLink;
	itLine->ResetIterator();
	while(itLine->HasNext())
		{
		TextLine* pTextLine = (TextLine*)itLine->GetNext();
		if(pTextLine)
			{
			TInt aPos = aRc.iTl.iX;
			TInt aLineHeight = pTextLine->GetLineRect().Height();
			
			Iterator* itTextBlock = (Iterator*)pTextLine->GetTextBlockArray();
			itTextBlock->ResetIterator();
			while(itTextBlock->HasNext())	
				{
				CTextBlock* pBlock = (CTextBlock*)itTextBlock->GetNext();
				if(pBlock)
					{
					TRect rcBlock = pBlock->GetBlockRect();
					TInt aTop = aLineHeight-rcBlock.Height();
					pBlock->SetLTPoint(aPos,aTop+top);
					
					aPos += rcBlock.Width();
					}
				}
			
			if(aPos > iFormatTextLineLink.iBound.iWidth)
				iFormatTextLineLink.iBound.iWidth = aPos;
			iFormatTextLineLink.iBound.iHeight += aLineHeight;

			top += aLineHeight;
			}
		}
}

void CStringSplitter::Draw(CGraphicsContext& aGc)
	{
	Iterator* itLine = (Iterator*)&iFormatTextLineLink;
	itLine->ResetIterator();
	while(itLine->HasNext())
		{
		TextLine* pTextLine = (TextLine*)itLine->GetNext();
		if(pTextLine)
			{
			Iterator* itTextBlock = (Iterator*)pTextLine->GetTextBlockArray();
			itTextBlock->ResetIterator();
			while(itTextBlock->HasNext())	
				{
				CTextBlock* pBlock = (CTextBlock*)itTextBlock->GetNext();
				if(pBlock)
					{
					pBlock->Draw(&aGc);
					}
				}
			
			}
		}
	}



